package com.baizhi.controller;

import com.baizhi.dto.StudentDTO;
import com.baizhi.entity.Student;
import com.baizhi.feign.FeignCity;
import com.baizhi.feign.FeignClazz;
import com.baizhi.feign.FeignGroup;
import com.baizhi.feign.FeignTag;
import com.baizhi.po.*;
import com.baizhi.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    private static final Logger log = LoggerFactory.getLogger(StudentController.class);
    @Autowired
    private StudentService studentService;
    @Autowired
    private FeignClazz feignClazz;
    @Autowired
    private FeignGroup feignGroup;
    @Autowired
    private FeignCity feignCity;
    @Autowired
    private FeignTag feignTag;

    @GetMapping
    public ArrayList<StudentPO> queryAll() {
        List<Student> students = studentService.queryAll();
        ArrayList<StudentPO> studentPOS = new ArrayList<>();
        for (Student student : students) {
            Long clazzid = student.getClazzid();
            ClazzsPO clazzsPO = feignClazz.queryById(clazzid);

            Long groupid = student.getGroupid();
            GroupsPO groupsPO = feignGroup.queryById(groupid);

            Long cityid = student.getCityid();
            CityPO cityPO = feignCity.queryById(cityid);

            ArrayList<Long> longs = feignTag.queryAll(student.getId());
            List<TagsPO> tagsPOS = new ArrayList<>();
            for (Long sid : longs) {
                TagsPO tagsPO = feignTag.queryAlls(sid);
                tagsPOS.add(tagsPO);
            }
            Long id = student.getId();
            String name = student.getName();
            Integer age = student.getAge();
            String qq = student.getQq();
            String phone = student.getPhone();
            Date bir = student.getBir();
            String starts = student.getStarts();
            String attr = student.getAttr();
            String mark = student.getMark();
            StudentPO studentPO = new StudentPO(id, name, age, qq, phone, bir, starts, attr, mark, clazzsPO, groupsPO, cityPO, tagsPOS);
            studentPOS.add(studentPO);

        }
        return studentPOS;
    }

    /**
     * 添加学生
     */
    @PostMapping
    public StudentsPO add(@RequestBody StudentDTO studentDTO) {
        log.info("studentDTO {}", studentDTO);
        Student student = studentService.add(studentDTO);
        System.out.println("添加成功了，下面要回显数据");
        /**
         * 根据学生信息查
         */
        Long clazzid = student.getClazzid();
        ClazzsPO clazzsPO = feignClazz.queryById(clazzid);
        String clazzName = clazzsPO.getName();

        Long groupid = student.getGroupid();
        GroupsPO group = feignGroup.queryById(groupid);
        String groupName = group.getName();

        Long cityid = student.getCityid();
        CityPO city = feignCity.queryById(cityid);
        String cityName = city.getName();

        List<Long> tagIds = studentDTO.getTagIds();
        ArrayList<String> strings = new ArrayList<>();
        for (Long tagId : tagIds) {
            TagsPO tagsPO = feignTag.queryAlls(tagId);
            String name = tagsPO.getName();
            strings.add(name);
        }

        StudentsPO studentsPO = new StudentsPO();
        studentsPO.setId(student.getId());
        studentsPO.setName(student.getName());
        studentsPO.setAge(student.getAge());
        studentsPO.setQq(student.getQq());
        studentsPO.setPhone(student.getPhone());
        studentsPO.setBir(student.getBir());
        studentsPO.setStarts(student.getStarts());
        studentsPO.setAttr(student.getAttr());
        studentsPO.setMark(student.getMark());
        studentsPO.setClazz(clazzName);
        studentsPO.setGroup(groupName);
        studentsPO.setCity(cityName);
        studentsPO.setTags(strings);
        return studentsPO;
    }
}
